package dk.mirasola.systemtraining.bridgewidgets.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import dk.mirasola.systemtraining.bridgewidgets.client.i18n.BridgeTexts;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Distribution;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Hand;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Seat;

public class DistributionView extends Composite {
    private BridgeTexts texts;
    private FlexTable outer;
    private HandPanel north;
    private HandPanel east;
    private HandPanel south;
    private HandPanel west;
    private Grid tableIcon;

    public DistributionView() {
        texts = (BridgeTexts) GWT.create(BridgeTexts.class);
        north = new HandPanel();
        east = new HandPanel();
        west = new HandPanel();
        south = new HandPanel();

        tableIcon = new Grid(3, 3);
        tableIcon.setHTML(0, 1, texts.northShort());
        tableIcon.setHTML(1, 0, texts.westShort());
        tableIcon.setHTML(1, 2, texts.eastShort());
        tableIcon.setHTML(2, 1, texts.southShort());
        tableIcon.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
        tableIcon.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_CENTER);
        tableIcon.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT);
        tableIcon.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_RIGHT);
        tableIcon.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
        tableIcon.getCellFormatter().setVerticalAlignment(2, 1, HasVerticalAlignment.ALIGN_BOTTOM);
        tableIcon.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_MIDDLE);
        tableIcon.getCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_MIDDLE);
        tableIcon.getCellFormatter().setWidth(0, 0, "33%");
        tableIcon.getCellFormatter().setWidth(0, 1, "33%");
        tableIcon.getCellFormatter().setWidth(0, 2, "33%");

        outer = new FlexTable();
        initWidget(outer);
        setStyleName("distributionView");

        outer.setWidget(0, 1, north);
        outer.setWidget(1, 0, west);
        outer.setWidget(1, 1, tableIcon);
        outer.setWidget(1, 2, east);
        outer.setWidget(2, 1, south);
        outer.getCellFormatter().setVerticalAlignment(1, 1, HasVerticalAlignment.ALIGN_MIDDLE);
        outer.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
        outer.getFlexCellFormatter().setColSpan(0, 1, 2);
        outer.getFlexCellFormatter().setColSpan(2, 1, 2);
    }

    public void setStyleName(String styleName) {
        super.setStyleName(styleName);
        tableIcon.setStyleName(styleName + "-tableIcon");
    }


    public void setDistribution(Distribution distribution) {
        Hand northHand = distribution.getHand(Seat.NORTH);
        north.setHand(northHand);
        north.setVisible(northHand != null);

        Hand eastHand = distribution.getHand(Seat.EAST);
        east.setHand(eastHand);
        east.setVisible(eastHand != null);

        Hand southHand = distribution.getHand(Seat.SOUTH);
        south.setHand(southHand);
        south.setVisible(southHand != null);

        Hand westHand = distribution.getHand(Seat.WEST);
        west.setHand(westHand);
        west.setVisible(westHand != null);
    }
}
